package com.zdp.contentcenter.service.content;

import com.zdp.contentcenter.dao.content.ShareMapper;
import com.zdp.contentcenter.domain.dto.content.ShareDTO;
import com.zdp.contentcenter.domain.dto.user.UserDTO;
import com.zdp.contentcenter.domain.entity.content.Share;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 *
 * 仅仅只是整合了ribbon时的服务接口
 *
 * @author sesshomaru
 * @date 2020/9/17 0:18
 */
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
@Service
public class RibbonShareService {

    private final ShareMapper shareMapper;

    private final RestTemplate restTemplate;

    private final DiscoveryClient discoveryClient;

    public ShareDTO findById(Integer id){
        // 获取分享详情
        Share share = this.shareMapper.selectByPrimaryKey(id);
        // 发布人id
        Integer userId = share.getUserId();

        // 怎么去调用用户微服务的/usrs/{id}
        UserDTO userDTO = restTemplate.getForObject("http://user-center/users/{id}",
                UserDTO.class, userId);

        ShareDTO shareDTO = new ShareDTO();
        BeanUtils.copyProperties(share ,shareDTO);
        shareDTO.setWxNickName(userDTO.getWxNickname());

        return shareDTO;
    }

    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();
        // http get方法去请求并且返回一个对象
        // 方法1
       /* String forObject = restTemplate.getForObject(
                "http://localhost:8080/users/2", String.class
        );*/
        // 方法2
       // uriVariables 的值会替换掉 {id}
       /* String forObject = restTemplate.getForObject(
                "http://localhost:8080/users/{id}",
                String.class,1);*/

        ResponseEntity<UserDTO> forEntity = restTemplate.getForEntity("http://localhost:8080/users/{id}",
                UserDTO.class, 1);
        UserDTO forObject = forEntity.getBody();
        System.out.println(forEntity.getStatusCode());

        System.out.println(forObject);
    }
}
